package com.pubinfo.passbook.common.service.engine.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pubinfo.passbook.common.entity.TEngineComputeChain;
import com.pubinfo.passbook.common.entity.TEngineComputeRule;
import com.pubinfo.passbook.common.exception.ServiceException;
import com.pubinfo.passbook.common.mapper.TEngineComputeChainMapper;
import com.pubinfo.passbook.common.mapper.TEngineComputeRuleMapper;
import com.pubinfo.passbook.common.model.dto.engine.EngineChainParam;
import com.pubinfo.passbook.common.service.engine.EngineComputeChainService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zhouch
 * @date 2021/12/14 11:19
 */
@Service
public class EngineComputeChainServiceImpl
        extends ServiceImpl<TEngineComputeChainMapper, TEngineComputeChain> implements EngineComputeChainService {

    @Resource
    TEngineComputeRuleMapper ruleMapper;

    @Override
    public TEngineComputeChain getChainInfoByRuleId(Integer ruleId) {
        TEngineComputeRule rule = ruleMapper.selectById(ruleId);

        if (rule == null) {
            throw new ServiceException("没有该计算规则ID: " + ruleId);
        }

        TEngineComputeChain chainInfo = baseMapper.selectById(rule.getChainId());

        if (chainInfo == null) {
            throw new ServiceException("没有该计算链ID: " + rule.getChainId());
        }

        return chainInfo;
    }

    @Override
    public IPage<TEngineComputeChain> page(EngineChainParam param) {
        Page<TEngineComputeChain> pager = param.getPage();
        return new LambdaQueryChainWrapper<>(baseMapper)
                .like(StringUtils.isNotBlank(param.getKeyword()), TEngineComputeChain::getName, param.getKeyword())
                .eq(StringUtils.isNotBlank(param.getChainType()), TEngineComputeChain::getType, param.getChainType())
                .page(pager);
    }

    @Override
    public TEngineComputeChain getByDistrictID(String districtId) {
        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(StringUtils.isNotBlank(districtId), TEngineComputeChain::getDistrictId, districtId)
                .one();
    }

    @Override
    public Boolean existDistrictID(String districtId) {

        if (districtId == null) {
            return false;
        }

        return new LambdaQueryChainWrapper<>(baseMapper)
                .eq(StringUtils.isNotBlank(districtId), TEngineComputeChain::getDistrictId, districtId)
                .count() > 0;
    }

    @Override
    public boolean updateById(TEngineComputeChain entity) {

        if (entity.getDistrictId() != null) {
            TEngineComputeChain currentChain = getByDistrictID(entity.getDistrictId());

            if (currentChain != null && !currentChain.getId().equals(entity.getId())) {
                throw new ServiceException("区县已经被绑定了, 无法修改.");
            }
        }


        return super.updateById(entity);
    }
}
